<template>
    <div style="padding: 10px;">
        <!--<Particles></Particles>-->
        <!--功能区域-->
        <div style="margin: 10px 0">
            <el-button type="success" round @click="add">新增</el-button>

        </div>

        <!--搜索-->
        <div style="margin: 10px; text-align: center;margin-left: -120px;border-radius: 20px">
            <el-input @keyup.enter.native="load" v-model="search" placeholder="请输入关键字"
                      style="border-radius: 20px;width: 20%;" clearable=""></el-input>
            <el-button type="primary" style="margin-left: 7px" @click="load">查询</el-button>
        </div>

        <el-table
                :data="tableData"
                border
                stripe=""
                size="medium"
                style="width: 100%">
            <el-table-column
                    width="80px"
                    prop="id"
                    sortable=""
                    label="ID">
            </el-table-column>
            <el-table-column
                    width="280px"
                    prop="name"
                    label="名称">
            </el-table-column>

            <el-table-column
                    width="280px"
                    prop="comment"
                    label="备注">
            </el-table-column>
            <el-table-column label="权限菜单">
                <template slot-scope="scope">
                    <el-select clearable v-model="scope.row.permissions" multiple placeholder="请选择" style="width: 80%">
                        <el-option v-for="item in permissions" :key="item.id" :label="item.comment" :value="item.id"></el-option>
                    </el-select>
                </template>
            </el-table-column>

            <el-table-column
                    width="250px"
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <!--保存权限-->
                    <el-button type="primary" size="mini" @click="handleChnage(scope.row)" >保存权限菜单</el-button>

                    <!--编辑-->
                    <el-button type="primary" @click="handleEdit(scope.row)" icon="el-icon-edit" circle></el-button>

                    <!--删除-->
                    <el-button @click="handleDelete(scope.row.id)"
                               slot="reference" type="danger" icon="el-icon-delete" circle
                               style="margin-left: 10px"></el-button>


                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage4"
                    :page-sizes="[5,10,20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>

            <!--新增-->
            <el-dialog

                    title="操作"
                    :visible.sync="dialogVisible"
                    width="30%">
                <el-form :model="form" label-width="120px">
                    <el-form-item label="名称">
                        <el-input v-model="form.name" style="width: 80%"></el-input>
                    </el-form-item>

                    <el-form-item label="备注">
                        <el-input v-model="form.comment" style="width: 80%"></el-input>
                    </el-form-item>
                </el-form>

                <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    // @ is an alias to /src

    import request from '../utils/request'



    export default {
        name: 'Role',

        data() {
            return {
                form: {},
                //新增
                dialogVisible: false,
                search: '',

                //当前页
                currentPage4: 1,
                pageSize: 5,
                total: 0,
                tableData: [],
                permissions: []
            }
        },
        created() {
            this.load()
        },
        methods: {
            //查询
            load() {
                request.get('/role/page', {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        search: this.search
                    }
                }).then(res => {
                    this.tableData = res.data.records
                    this.total = res.data.total
                })

                request.get("/permission/all").then(res => {
                    this.permissions = res.data
                })
            },

            //新增
            add() {
                this.dialogVisible = true
                this.form = {} //清空输入的表单
                this.load()


            },
            //保存到后台
            save() {


                if (this.form.id) {
                    //    更新
                    request.put("/role", this.form).then(res => {
                        if (res.code == 20000) {
                            this.$toast({
                                message: '更新成功',
                                type: 'success',

                            });
                            this.load()
                        } else {
                            this.$toast({

                                message: res.msg,
                                type: 'error',

                            })
                        }

                    })
                    this.dialogVisible = false
                    this.load()
                } else {
                    //    新增
                    let userStr = sessionStorage.getItem("user") || "{}"
                    let user = JSON.parse(userStr)
                    this.form.author = user.nick

                    request.post("/role", this.form).then(res => {

                        this.dialogVisible = false
                        if (res.code == 20000) {


                            this.$toast({
                                message: '添加成功',
                                type: 'success',

                            });
                            this.load()
                        }

                        else {
                            this.$toast({

                                message: res.msg,
                                type: 'error',

                            })
                        }
                    })
                }


            },
            //保存权限信息
            handleChnage(row){
                request.put("/role/changePermission",row).then(res=>{
                    if (res.code===20000){
                        this.$toast({type: 'success', message: '更新成功!'})

                        //当前登录的用户角色id如果等于当前操作行角色的id，那么就需要重新登录
                        if (res.data===true) {
                            this.$router.push('/login')
                        }

                    }
                })

            },
            //编辑
            handleEdit(row) {
                //缓冲
                this.form = JSON.parse(JSON.stringify(row))


                this.dialogVisible = true

            },
            handleDelete(id) {

                this.$confirm('你确定要删除,是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    request.delete("/role/" + id).then(res => {
                        console.log(res.code)
                        this.$toast({type: 'success', message: '删除成功!'})
                        this.load()
                    })
                }).catch(() => {
                    this.$toast({type: 'success', message: '已取消删除'})
                })


            },
            //页码 每页的个数
            handleSizeChange(pageSize) {
                this.pageSize = pageSize
                this.load()
            },
            //改变当前页
            handleCurrentChange(pageNum) {
                this.currentPage = pageNum
                this.load()
            },

        }
    }
</script>

<style scoped>

    el-input {
        border-radius: 20px
    }
</style>
